﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GranitePackageSystem.FileUploader;
using GranitePackageSystem.GranitePackagingService;
using GranitePackageSystem.Controls;
using System.Xml;
using System.Xml.Linq;


namespace GranitePackageSystem.Views.Packaging
{
    public partial class Request : Page
    {
        private int reqNumber = -1;
        System.Windows.Controls.BusyIndicator busyIndicator = new System.Windows.Controls.BusyIndicator();
        public Request()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnStartRequest_Click(object sender, RoutedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.setupRequestCompleted += new EventHandler<setupRequestCompletedEventArgs>(proxy_setupRequestCompleted);
            proxy.setupRequestAsync();

            this.busyIndicator.IsBusy = true;
        }

        void proxy_setupRequestCompleted(object sender, setupRequestCompletedEventArgs e)
        {
            reqNumber = e.Result;
            
            this.busyIndicator.IsBusy = false;

            ChildWindows.UploadFiles fileUploader = new ChildWindows.UploadFiles(reqNumber);
            fileUploader.Closed += new EventHandler(fileUploader_Closed);
            fileUploader.Show();
        }

        void fileUploader_Closed(object sender, EventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();

            this.busyIndicator.IsBusy = true;

            this.borderPackageDetails.Visibility = System.Windows.Visibility.Visible;
            this.panelPackageDetails.Visibility = System.Windows.Visibility.Visible;

            proxy.getSourceFileObjectCompleted += new EventHandler<getSourceFileObjectCompletedEventArgs>(proxy_getSourceFileObjectCompleted);
            proxy.getSourceFileObjectAsync(this.reqNumber.ToString());
        }

        void proxy_getSourceFileObjectCompleted(object sender, getSourceFileObjectCompletedEventArgs e)
        {
            this.busyIndicator.IsBusy = false;
            
            this.treeViewSoftware.Visibility = System.Windows.Visibility.Visible;
            this.labelInstallItem.Visibility = System.Windows.Visibility.Visible;
            this.boxInstallItem.Visibility = System.Windows.Visibility.Visible;
            
            this.treeViewSoftware.ItemsSource = e.Result;
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            if (this.reqNumber != -1)
            {
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.teardownRequestAsync(reqNumber.ToString());
            }
        }

        private void treeViewSoftware_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            fNode nodeValue = (fNode) e.NewValue;

            string fileExtension = getFileExtension(nodeValue.Name);

            if (nodeValue.isFile)
            {
                if(fileExtension.ToLower().Equals("msi"))
                {
                    MessageBox.Show("Selected File is a MSI File, interrogating for properties");
                }
                boxInstallItem.Text = nodeValue.Name;
            }
        }

        private string getFileExtension(string fileName)
        {
            string[] fileNameArray = fileName.Split('.');
            string fileExtension = fileNameArray[fileNameArray.Length - 1];

            return fileExtension;
        }
    }
}
